//
//  ScenicCell.m
//  51-Tour
//
//  Created by xiang ying on 14-3-27.
//  Copyright (c) 2014年 xiang ying. All rights reserved.
//

#import "ScenicCell.h"
#import "DownloadManagement.h"
#import "ServiceManage.h"
#import "NSString+TPCategory.h"
#import "UIImageView+WebCache.h"

@interface ScenicCell(){
    UIImageView     *_icon;
    UILabel         *_nameLabel;
    UILabel         *_sizeLabel;
    UIButton        *_downloadBtn;
    ScenicScene     *_scenic;
}

@end

@implementation ScenicCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        //
         _icon = [[UIImageView alloc] initWithFrame:CGRectMake(5, 10, ScenicCellHeight-20, ScenicCellHeight-20)];
        _icon.layer.borderWidth = 1;
        _icon.layer.borderColor = BORDERCOLOR.CGColor;
        _icon.image = [UIImage imageNamed:@"jd_09"];
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(1, 1, _icon.frame.size.width-2, _icon.frame.size.height-2);
        layer.borderWidth = 1;
        layer.borderColor = [UIColor whiteColor].CGColor;
        [_icon.layer addSublayer:layer];
        [self addSubview:_icon];
        
        //
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(_icon.frame.origin.x+_icon.frame.size.width+3, ScenicCellHeight/2-14, 160, 24)];
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.text = @"中山陵";
        _nameLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_nameLabel];
        
        //
        _sizeLabel = [[UILabel alloc] initWithFrame:CGRectMake(_nameLabel.frame.origin.x, _nameLabel.frame.origin.y+_nameLabel.frame.size.height, _nameLabel.frame.size.width, _nameLabel.frame.size.height)];
        _sizeLabel.backgroundColor = [UIColor clearColor];
        _sizeLabel.textColor = THEMECOLOR;
        _sizeLabel.text = @"10M";
        _sizeLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:_sizeLabel];
        
        //
        _downloadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_downloadBtn setTitleColor:THEMECOLOR forState:UIControlStateNormal];
        _downloadBtn.frame = CGRectMake(320-80-10, ScenicCellHeight-26-10, 80, 26);
        _downloadBtn.layer.borderWidth = 1;
        _downloadBtn.layer.borderColor = THEMECOLOR.CGColor;
        _downloadBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        [_downloadBtn setTitle:@"下载" forState:UIControlStateNormal];
        [self addSubview:_downloadBtn];
        
        UIImageView *_line = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"list-line_14"]];
        _line.frame = CGRectMake(0, ScenicCellHeight-1, 320, 1);
        [self addSubview:_line];
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)updateWithData:(ScenicScene*)data{
     _scenic = data;
    
    @try {
        [_scenic removeObserver:self forKeyPath:@"progress"];
        [_scenic removeObserver:self forKeyPath:@"isDownloading"];
    }
    @catch (NSException *exception) {
        
    }
    @finally {
        
    }
    
    if (_scenic.iconImage) {
        _icon.image = _scenic.iconImage;
    }else{
        if ([_scenic.icon isURLString]) {
            __block UIImageView *icon = _icon;
            __block ScenicScene *scenic = _scenic;
            [_icon setImageWithURL:[NSURL URLWithString:_scenic.icon] placeholderImage:[UIImage imageNamed:@"jd_09"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                if (image) {
                    icon.image = image;
                    scenic.iconImage = image;
                }
            }];
        }else{
            _icon.image = [UIImage imageNamed:@"jd_09"];
            _scenic.iconImage = [UIImage imageNamed:@"jd_09"];
        }
    }
    
    [_scenic addObserver:self forKeyPath:@"progress" options:NSKeyValueObservingOptionNew context:nil];
    [_scenic addObserver:self forKeyPath:@"isDownloading" options:NSKeyValueObservingOptionNew context:nil];
    _nameLabel.text = data.scenicName;
    _sizeLabel.text = [NSString stringWithFormat:@"%.2fM",data.zipSize/1024/1024];
    [_downloadBtn removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
    if (_scenic.isDownloading) {
        [self setDownloadBtnState:3];
    }else{
        if (_scenic.needUpdate) {
            [self setDownloadBtnState:4];
        }else if(_scenic.progress ==0){
            [self setDownloadBtnState:0];
        }else if (_scenic.progress == _scenic.zipSize) {
            [self setDownloadBtnState:1];
        } else{
            [self setDownloadBtnState:2];
        }
    }
}


-(void)setDownloadBtnState:(int)state{
    if (state == 0) {
        [_downloadBtn setTitle:[[DataCenter instance] getStringForKey:@"Download"] forState:UIControlStateNormal];
        [_downloadBtn setTitleColor:THEMECOLOR forState:UIControlStateNormal];
        _downloadBtn.layer.borderColor = THEMECOLOR.CGColor;
        [_downloadBtn addTarget:self action:@selector(downloadTask:) forControlEvents:UIControlEventTouchUpInside];
    }else if(state == 1){
        [_downloadBtn removeTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside];
        [_downloadBtn setTitle:[[DataCenter instance] getStringForKey:@"DownloadComplete"] forState:UIControlStateNormal];
        [_downloadBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        _downloadBtn.layer.borderColor = [UIColor darkGrayColor].CGColor;
    }else if(state == 2){
        [_downloadBtn removeTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside];
        [_downloadBtn setTitle:[[DataCenter instance] getStringForKey:@"paused"] forState:UIControlStateNormal];
        [_downloadBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _downloadBtn.layer.borderColor = [UIColor redColor].CGColor;
    }else if(state == 3){
        [_downloadBtn removeTarget:self action:NULL forControlEvents:UIControlEventTouchUpInside];
        [_downloadBtn setTitle:[[DataCenter instance] getStringForKey:@"Downloading"] forState:UIControlStateNormal];
        [_downloadBtn setTitleColor:THEMECOLOR forState:UIControlStateNormal];
        _downloadBtn.layer.borderColor = THEMECOLOR.CGColor;
    }else if (state == 4){
        [_downloadBtn setTitle:[[DataCenter instance] getStringForKey:@"update"] forState:UIControlStateNormal];
        [_downloadBtn setTitleColor:THEMECOLOR forState:UIControlStateNormal];
        _downloadBtn.layer.borderColor = THEMECOLOR.CGColor;
        [_downloadBtn addTarget:self action:@selector(downloadTask:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(void)downloadTask:(id)sender{
    if ([ServiceManage checkNet]) {
        _scenic.isDownloading = YES;
        //建立下载
        [[DownloadManagement instance] addTarget:_scenic];
        [self setDownloadBtnState:3];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[[DataCenter instance] getStringForKey:@"Tips"] message:@"当前没有网络" delegate:nil cancelButtonTitle:[[DataCenter instance] getStringForKey:@"Sure"] otherButtonTitles: nil];
        [alert show];
    }
}


- (void)dealloc{
    @try {
        [_scenic removeObserver:self forKeyPath:@"progress"];
        [_scenic removeObserver:self forKeyPath:@"isDownloading"];
    }
    @catch (NSException *exception) {
        
    }
    @finally {
        
    }
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"progress"]) {
        if (_scenic.progress == _scenic.zipSize){
            //下载完成
            [self setDownloadBtnState:1];
        }else if(_scenic.progress == 0){
            [self setDownloadBtnState:0];
        }
    }else{
        if (!_scenic.isDownloading) {
            if (_scenic.progress == 0) {
                [self setDownloadBtnState:0];
            }else if(_scenic.progress == _scenic.zipSize){
                [self setDownloadBtnState:1];
            }else{
                [self setDownloadBtnState:2];
            }
        }else{
            [self setDownloadBtnState:3];
        }
    }
}

@end
